<?php
namespace App\Http\Controllers\Index;

use App\Http\Responses\DemoResp;
use Illuminate\Support\Facades\Log;
//use Laravel\Lumen\Routing\Controller;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Logic\finger\UserLogic;

class AuthController extends Controller
{

    private static $userModel = null;

    private $auth = null;

    protected $currentDateTime;

    public function __construct()
    {
        parent::__construct();
        $this->getInstance();
        $this->currentDateTime = time();
    }

    public function getInstance(){
        if( !self::$userModel ){
            self::$userModel  = new User;
        }
    }
    /**
     * @OA\Post(
     *     path="/login",
     *     tags={"用户登录"},
     *     summary="用户登录",
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"username", "password"},
     *                 @OA\Property(property="username", type="string", description="手机号",default="213"),
     *                 @OA\Property(property="password", type="string", description="密码"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function login(Request $request)
    {
        $post_data = $request->all();
        return UserLogic::login($post_data);
    }

    /**
     * @OA\Post(
     *     path="/fog",
     *     tags={"用户登录"},
     *     summary="忘记密码",
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"phone", "password","code"},
     *                 @OA\Property(property="phone", type="string", description="手机号",default="213"),
     *                 @OA\Property(property="password", type="string", description="密码"),
     *                 @OA\Property(property="code", type="string", description="验证码"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function fog(Request $request)
    {
        $post_data = $request->all();
        $scene = $request->path();
        return User::fog($post_data,$scene);
    }

    /**
     * @OA\Post(
     *     path="/user_info",
     *     tags={"用户登录"},
     *     summary="用户信息",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function userInfo()
    {
        return s_msg('',$this->user);
//        return self::$userModel->auth_user();
    }


    /**
     * @OA\Post(
     *     path="/logout",
     *     tags={"用户登录"},
     *     summary="用户退出（清除token）",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function logout()
    {
        return self::$userModel->logout();
    }

    /**
     * @OA\Post(
     *     path="/refresh",
     *     tags={"用户登录"},
     *     summary="用户刷新token",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function refresh_token()
    {
        $response = array('status_code' => '2000');

        $this->initialAuth();

        if ($token = $this->auth->refresh(true, true)) {
            $response['data'] = [
                'access_token' => $token,
                'expires_in' => $this->currentDateTime + 86400 # 24小时过期
            ];
        } else {
            $response = [
                'status_code' => '5000',
                'msg' => '系统错误，无法生成令牌'
            ];
        }

        return response()->json($response);
    }

    /**
     * @OA\Post(
     *     path="/register",
     *     tags={"用户登录"},
     *     summary="用户注册",
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"phone", "password","inviter","code"},
     *                 @OA\Property(property="phone", type="string", description="手机号",default="213"),
     *                 @OA\Property(property="password", type="string", description="密码"),
     *                 @OA\Property(property="code", type="string", description="验证码")
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function register(Request $request){

        $post_data = $request->all();
        $scene = $request->path();
        return UserLogic::add($post_data,$scene);
    }


    private function initialAuth()
    {
        $this->auth = app('auth')->guard('api');
        return;
    }
}
